package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TowerLevelMapper;
import com.ruoyi.system.domain.TowerLevel;
import com.ruoyi.system.service.ITowerLevelService;

/**
 * 二层展示Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-31
 */
@Service
public class TowerLevelServiceImpl implements ITowerLevelService 
{
    @Autowired
    private TowerLevelMapper towerLevelMapper;

    /**
     * 查询二层展示
     * 
     * @param id 二层展示主键
     * @return 二层展示
     */
    @Override
    public TowerLevel selectTowerLevelById(Integer id)
    {
        return towerLevelMapper.selectTowerLevelById(id);
    }

    /**
     * 查询二层展示列表
     * 
     * @param towerLevel 二层展示
     * @return 二层展示
     */
    @Override
    public List<TowerLevel> selectTowerLevelList(TowerLevel towerLevel)
    {
        return towerLevelMapper.selectTowerLevelList(towerLevel);
    }

    /**
     * 新增二层展示
     * 
     * @param towerLevel 二层展示
     * @return 结果
     */
    @Override
    public int insertTowerLevel(TowerLevel towerLevel)
    {
        //判断当前层级是否存在
        TowerLevel level = towerLevelMapper.selectTowerLevelByLevel(towerLevel.getTowerId(),towerLevel.getTowerLevel());
        if(level!=null){
            throw new RuntimeException("当前层级已存在");
        }
        //判断是否超过最大层级
        int maxLevel = towerLevelMapper.selectMaxLevel(towerLevel.getTowerId());
        if(towerLevel.getTowerLevel()>maxLevel){
            throw new RuntimeException("超过最大层级,请重新选择");
        }
        //判断总层数是否超过最大层级
        int sumLevel = towerLevelMapper.selectSumLevel(towerLevel.getTowerId());
        if(sumLevel>=maxLevel){
            throw new RuntimeException("总层数超过最大层级,已不可再添加");
        }
        return towerLevelMapper.insertTowerLevel(towerLevel);
    }

    /**
     * 修改二层展示
     * 
     * @param towerLevel 二层展示
     * @return 结果
     */
    @Override
    public int updateTowerLevel(TowerLevel towerLevel)
    {
        //判断是否超过最大层级
        int maxLevel = towerLevelMapper.selectMaxLevel(towerLevel.getTowerId());
        if(towerLevel.getTowerLevel()>maxLevel){
            throw new RuntimeException("超过最大层级,请重新选择");
        }
        return towerLevelMapper.updateTowerLevel(towerLevel);
    }

    /**
     * 批量删除二层展示
     * 
     * @param ids 需要删除的二层展示主键
     * @return 结果
     */
    @Override
    public int deleteTowerLevelByIds(Integer[] ids)
    {
        return towerLevelMapper.deleteTowerLevelByIds(ids);
    }

    /**
     * 删除二层展示信息
     * 
     * @param id 二层展示主键
     * @return 结果
     */
    @Override
    public int deleteTowerLevelById(Integer id)
    {
        return towerLevelMapper.deleteTowerLevelById(id);
    }
}
